﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AspNetCs.Model;
using AspNetCs.IDAL;
using System.Data.SqlClient;
using AspNetCs.Common;
using System.Web;

namespace AspNetCs.SQLServerDAL
{
    public class AgentDAL:IAgent
    {

        #region IAgent 成员
        /// <summary>
        /// 代理商登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AgentLogin(AgentModel model)
        {
            int IsLogin = -1;
            db Conn = new db();
            try
            {
                SqlDataReader sdr = Conn.GetSqlDataReader("Select Top 1 * from Agent where AgentUserName='" + model.AgentUserName.Trim().ToLower() + "' and AgentPwd='" + EncryptString.GetEncrypt(model.AgentPwd.Trim().ToLower()) + "'");
                if (sdr.Read())
                {
                    IsLogin = 0;//用户名密码验证成功
                    UpdateAgentLast(sdr.GetInt32(0));
                    HttpContext.Current.Session["AgentInfo"] = GetModel(sdr.GetInt32(0));
                }

            }
            catch
            {
                IsLogin = -1;//用户名,密码错误
            }
            finally
            {
                Conn.close();
            }
            return IsLogin;



        }
        /// <summary>
        /// 根据DATAROW,返回代理商资料
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public AgentModel GetModel(DataRow dr)
        {
            AgentModel model = new AgentModel();
            if (dr["Id"] != null) { model.Id = Convert.ToInt32(dr["Id"].ToString()); }
            if (dr["AgentUserName"] != null) { model.AgentUserName = dr["AgentUserName"].ToString(); }
            if (dr["AgentPwd"] != null) { model.AgentPwd = dr["AgentPwd"].ToString(); }
            if (dr["Purview"] != null) { model.Purview = dr["Purview"].ToString(); }
            if (dr["TrueName"] != null) { model.TrueName = dr["TrueName"].ToString(); }
            if (dr["LoginPro"] != null) { model.LoginPro = Convert.ToInt32(dr["LoginPro"].ToString()); }
            if (dr["LoginCity"] != null) { model.LoginCity = Convert.ToInt32(dr["LoginCity"].ToString()); }
            if (dr["Disu"] != null) { model.Disu = Convert.ToInt32(dr["Disu"].ToString()); }
            if (dr["LoginIP"] != null) { model.LoginIP = dr["LoginIP"].ToString(); }
            if (dr["LoginTime"] != null) { model.LoginTime =DateTime.Parse(dr["LoginTime"].ToString()); }
            if (dr["RegTime"] != null) { model.RegTime = DateTime.Parse(dr["RegTime"].ToString()); }
            if (dr["LoginCount"] != null) { model.LoginCount = Convert.ToInt32(dr["LoginCount"].ToString()); }
            if (dr["Company"] != null) { model.Company = dr["Company"].ToString(); }
            if (dr["Email"] != null) { model.Email = dr["Email"].ToString(); }
            if (dr["Tel"] != null) { model.Tel = dr["Tel"].ToString(); }
            if (dr["Zip"] != null) { model.Zip = dr["Zip"].ToString(); }
            if (dr["Fax"] != null) { model.Fax = dr["Fax"].ToString(); }
            if (dr["Address"] != null) { model.Address = dr["Address"].ToString(); }
            if (dr["IsCheck"] != null) { model.IsCheck =StringHelper.isNumber_int(dr["IsCheck"].ToString()); }
            if (dr["Forms"] != null) { model.Forms =StringHelper.isNumber_int(dr["Forms"].ToString()); }
            if (dr["IsLock"] != null) { model.IsLock =StringHelper.isNumber_int(dr["IsLock"].ToString()); }
            
            return model;
        }


        /// <summary>
        /// 添加代理商
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddAgent(AgentModel model)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[20];

            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@AgentUserName";
            paras[0].objValue = model.AgentUserName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@AgentPwd";
            paras[1].objValue = model.AgentPwd;

            paras[2].dbType = SqlDbType.Text;
            paras[2].parName = "@Purview";
            paras[2].objValue = model.Purview;

            paras[3].dbType = SqlDbType.VarChar;
            paras[3].parName = "@TrueName";
            paras[3].objValue = model.TrueName;

            paras[4].dbType = SqlDbType.VarChar;
            paras[4].parName = "@Company";
            paras[4].objValue = model.Company;


            paras[5].dbType = SqlDbType.Char;
            paras[5].parName = "@Zip";
            paras[5].objValue = model.Zip;

            paras[6].dbType = SqlDbType.VarChar;
            paras[6].parName = "@Email";
            paras[6].objValue = model.Email;

            paras[7].dbType = SqlDbType.VarChar;
            paras[7].parName = "@Tel";
            paras[7].objValue = model.Tel;


            paras[8].dbType = SqlDbType.VarChar;
            paras[8].parName = "@Fax";
            paras[8].objValue = model.Fax;

            paras[9].dbType = SqlDbType.VarChar;
            paras[9].parName = "@Address";
            paras[9].objValue = model.Address;

            paras[10].dbType = SqlDbType.Int;
            paras[10].parName = "@IsCheck";
            paras[10].objValue = model.IsCheck;

            paras[11].dbType = SqlDbType.VarChar;
            paras[11].parName = "@LoginIP";
            paras[11].objValue = model.LoginIP;

            paras[12].dbType = SqlDbType.DateTime;
            paras[12].parName = "@LoginTime";
            paras[12].objValue = model.LoginTime;

            paras[13].dbType = SqlDbType.Int;
            paras[13].parName = "@LoginCount";
            paras[13].objValue = model.LoginCount;

            paras[14].dbType = SqlDbType.Int;
            paras[14].parName = "@LoginPro";
            paras[14].objValue = model.LoginPro;

            paras[15].dbType = SqlDbType.Int;
            paras[15].parName = "@LoginCity";
            paras[15].objValue = model.LoginCity;

            paras[16].dbType = SqlDbType.DateTime;
            paras[16].parName = "@RegTime";
            paras[16].objValue = model.RegTime;

            paras[17].dbType = SqlDbType.Int;
            paras[17].parName = "@Disu";
            paras[17].objValue = model.Disu;

            paras[18].dbType = SqlDbType.Int;
            paras[18].parName = "@Forms";
            paras[18].objValue = model.Forms;

            paras[19].dbType = SqlDbType.Int;
            paras[19].parName = "@IsLock";
            paras[19].objValue = model.IsLock;

            string cmdText = "insert into Agent values (@AgentUserName,@AgentPwd,@Purview,@TrueName,@Company,@Zip,@Email,@Tel,@Fax,@Address";
            cmdText += ",@IsCheck,@LoginIP,@LoginTime,@LoginCount,@LoginPro,@LoginCity,@RegTime,@Disu,@Forms,@IsLock)";
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText,paras);

            return count;
        }
        /// <summary>
        /// 修改代理商
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateAgent(AgentModel model)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[21];

            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@AgentUserName";
            paras[0].objValue = model.AgentUserName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@AgentPwd";
            paras[1].objValue = model.AgentPwd;

            paras[2].dbType = SqlDbType.Text;
            paras[2].parName = "@Purview";
            paras[2].objValue = model.Purview;

            paras[3].dbType = SqlDbType.VarChar;
            paras[3].parName = "@TrueName";
            paras[3].objValue = model.TrueName;

            paras[4].dbType = SqlDbType.VarChar;
            paras[4].parName = "@Company";
            paras[4].objValue = model.Company;


            paras[5].dbType = SqlDbType.Char;
            paras[5].parName = "@Zip";
            paras[5].objValue = model.Zip;

            paras[6].dbType = SqlDbType.VarChar;
            paras[6].parName = "@Email";
            paras[6].objValue = model.Email;

            paras[7].dbType = SqlDbType.VarChar;
            paras[7].parName = "@Tel";
            paras[7].objValue = model.Tel;


            paras[8].dbType = SqlDbType.VarChar;
            paras[8].parName = "@Fax";
            paras[8].objValue = model.Fax;

            paras[9].dbType = SqlDbType.VarChar;
            paras[9].parName = "@Address";
            paras[9].objValue = model.Address;

            paras[10].dbType = SqlDbType.Int;
            paras[10].parName = "@IsCheck";
            paras[10].objValue = model.IsCheck;

            paras[11].dbType = SqlDbType.VarChar;
            paras[11].parName = "@LoginIP";
            paras[11].objValue = model.LoginIP;

            paras[12].dbType = SqlDbType.DateTime;
            paras[12].parName = "@LoginTime";
            paras[12].objValue = model.LoginTime;

            paras[13].dbType = SqlDbType.Int;
            paras[13].parName = "@LoginCount";
            paras[13].objValue = model.LoginCount;

            paras[14].dbType = SqlDbType.Int;
            paras[14].parName = "@LoginPro";
            paras[14].objValue = model.LoginPro;

            paras[15].dbType = SqlDbType.Int;
            paras[15].parName = "@LoginCity";
            paras[15].objValue = model.LoginCity;

            paras[16].dbType = SqlDbType.DateTime;
            paras[16].parName = "@RegTime";
            paras[16].objValue = model.RegTime;

            paras[17].dbType = SqlDbType.Int;
            paras[17].parName = "@Disu";
            paras[17].objValue = model.Disu;

            paras[18].dbType = SqlDbType.Int;
            paras[18].parName = "@Forms";
            paras[18].objValue = model.Forms;

            paras[19].dbType = SqlDbType.Int;
            paras[19].parName = "@IsLock";
            paras[19].objValue = model.IsLock;

            paras[20].dbType = SqlDbType.Int;
            paras[20].parName = "@Id";
            paras[20].objValue = model.Id;

            string cmdText = "update Agent set AgentUserName=@AgentUserName,AgentPwd=@AgentPwd,Purview=@Purview,TrueName=@TrueName,Company=@Company";
            cmdText+=",Zip=@Zip,Email=@Email,Tel=@Tel,Fax=@Fax,Address=@Address";
            cmdText += ",IsCheck=@IsCheck,LoginIP=@LoginIP,LoginTime=@LoginTime,LoginCount=@LoginCount,LoginPro=@LoginPro,LoginCity=@LoginCity,RegTime=@RegTime,";
            cmdText += "Disu=@Disu,Forms=@Forms,IsLock=@IsLock where Id=@Id";
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }

        #endregion


        /// <summary>
        /// 根据ID,返回代理商资料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AgentModel GetModel(int id)
        {
            AgentModel model = new AgentModel();
            db Cn = new db();
            DataTable dt = Cn.GetSql_DataTable("Select Top 1 * from Agent  Where Id=" + id);
            if (dt.Rows.Count > 0)
            {
                model = GetModel(dt.Rows[0]);

            }
            return model;
        }


        /// <summary>
        /// 当代理商登录以后,更新代理商最后登录资料
        /// </summary>
        /// <param name="id"></param>
        public void UpdateAgentLast(int id)
        {
            db ConnUpdate = new db();
            string Sql = "Update Agent Set LoginCount=LoginCount+1,LoginTime='" + DateTime.Now + "',LoginIP='" + StringHelper.Ip() + "'  where Id=" + id.ToString();
            ConnUpdate.Execute_Noget(Sql);
            ConnUpdate.close();
        }

        public DataTable GetDataTable(string filter)
        {
            DataTable dataTable = new DataTable();
            string cmdText = "select *from Agent ";
            if(!string.IsNullOrEmpty(filter))
            {
                cmdText += "where "+filter;
            }
            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText);
            return dataTable;
        }

    }
}
